package com.easylinkin.linkappapi.bim.entity;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.enums.IdType;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.FieldNameConstants;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.Date;

/**
 * bim项目任务（计划）表(BimTask)表实体类
 *
 * @author pwqoc
 * @since 2024-05-24 15:02:33
 */
@EqualsAndHashCode(callSuper = true)
@Data
@TableName("bim_task")
@FieldNameConstants
public class BimTask extends Model<BimTask> {
    //主键id
    @TableId(type = IdType.INPUT)
    private String id;
    //项目导入记录id
    private String importRecordId;
    //任务Id
    private Integer taskId;
    //上级任务Id
    private Integer taskPid;
    //结构层级
    private Integer level;
    //任务名称
    private String taskName;
    //任务全称
    private String taskFullName;
    //工期
    private String duration;

    public void setDuration() {
        if (startDate != null && endDate != null) {
            // 计算相差的总天数
            long days = DateUtil.between(startDate, endDate, DateUnit.DAY);

            // 计算相差的完整周数
            int weeks = (int) Math.floor((double) days / 7);

            // 获取剩余的天数，即不足一周的天数
            int remainingDays = (int) (days % 7);

            this.duration = weeks + "周" + remainingDays + "天";
        }
    }

    //开始时间
    private Date startDate;
    //结束时间
    private Date endDate;
    //租户id
    private String tenantId;
    //创建人id
    private String creatorId;
    //创建人
    private String creator;
    //创建时间
    private Date createTime;
    //修改人id
    private String modifyId;
    //修改人
    private String modifier;
    //修改时间
    private Date modifyTime;
    //删除标记 1未删除 0已删除
    @TableLogic(value = "1", delval = "0")
    private Integer deleteState;


    public static final String R_ID = "rid";
    public static final String COMPLETION_STATUS = "completionStatus";

    public BigDecimal getCompletionStatus() {
        if (startDate == null || endDate == null) {
            return BigDecimal.ZERO;
        }
        DateTime now = new DateTime(LocalDate.now());
        if (DateUtil.compare(now, startDate) <= 0) {
            return BigDecimal.ZERO;

        }
        if (DateUtil.compare(now, endDate) >= 0) {
            return BigDecimal.valueOf(100);
        }

        // 计算相差的总天数
        long days = DateUtil.between(startDate, endDate, DateUnit.DAY);
        long currentDays = DateUtil.between(startDate, now, DateUnit.DAY);

        if (days == 0) {
            return BigDecimal.ZERO;
        } else {
            BigDecimal divide = BigDecimal.valueOf(currentDays).divide(BigDecimal.valueOf(days), 4, RoundingMode.HALF_UP);
            return divide.multiply(BigDecimal.valueOf(100)).setScale(2, RoundingMode.HALF_UP);
        }
    }

}

